package org.password.controller;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventTarget;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.input.*;
import org.password.entity.PasswordInfo;
import org.password.utils.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class PasswordController {
    @FXML
    public TableView<PasswordInfo> passwordTable;

    @FXML
    public TextField passwordField;
    @FXML
    public TextField accountField;
    @FXML
    public TextField websiteField;

    @FXML
    private Label welcomeText;

    @FXML
    protected void onHelloButtonClick() {
        welcomeText.setText("Welcome to JavaFX Application!");
    }

    public void initialize() {

        DBUtil dbUtil = new DBUtil();
        List<PasswordInfo> data1 = dbUtil.getData();

        // 创建一个ObservableList来保存表格数据
        ObservableList<PasswordInfo> data = FXCollections.observableArrayList(data1);

        // 将数据设置给表格
        passwordTable.setItems(data);

        passwordTable.getSelectionModel().setCellSelectionEnabled(true);


    }

    @FXML
    public void handleCopyEvent(KeyEvent event) {
        if (event.isControlDown() && event.getCode() == KeyCode.C) {
            TablePosition<?, ?> pos = passwordTable.getSelectionModel().getSelectedCells().get(0);
            int row = pos.getRow();
            int column = pos.getColumn();
            TableColumn<?, ?> tableColumn = pos.getTableColumn();
            String value = (String) tableColumn.getCellObservableValue(row).getValue();
            if (value != null) {
                Clipboard clipboard = Clipboard.getSystemClipboard();
                ClipboardContent content = new ClipboardContent();
                content.putString(value);
                clipboard.setContent(content);
            }
        }
    }

    @FXML
    public void savePassword() {
        String website = websiteField.getText();
        String account = accountField.getText();
        String password = passwordField.getText();

        DBUtil dbUtil = new DBUtil();
        Boolean aBoolean = dbUtil.insertData(website, account, password);
        if (aBoolean) {
            websiteField.setText("");
            accountField.setText("");
            passwordField.setText("");
        }
    }

    public void refresh(Event event) {
        initialize();
    }

    public void selectData(MouseEvent event) {
        Object source = event.getSource();
        if (source instanceof TableView) {
            TableView<?> tableView = (TableView<?>) source;
            for (TablePosition<?, ?> selectedCell : tableView.getSelectionModel().getSelectedCells()) {
                int row = selectedCell.getRow();
                int column = selectedCell.getColumn();
                if (column != 0) continue;
                TableColumn<?, ?> tableColumn = selectedCell.getTableColumn();
                TableCell<?,?> target = (TableCell<?, ?>)event.getTarget();

                if (!target.isEmpty()) {
                    if (target.getIndex() == row && target.getTableColumn() == tableColumn) {
                        Object o2 = tableView.getItems().get(row);
                        if (o2 instanceof PasswordInfo) {
                            PasswordInfo o1 = (PasswordInfo) o2;
                            o1.setChecked(!o1.isChecked());
                        }
                    }
                }
            }

        }
    }

    public void deletePassword(ActionEvent actionEvent) {
        ObservableList<PasswordInfo> items = passwordTable.getItems();
        ArrayList<String> ids = new ArrayList<>();
        for (PasswordInfo item : items) {
            if (!item.isChecked()) continue;
            ids.add(item.getId());
        }
        if (ids.size() == 0) return;
        DBUtil dbUtil = new DBUtil();
        boolean b = dbUtil.delPassword(ids);
        if (b) {
            initialize();
        }
    }
}
